package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class PermissionsActivity extends Activity {
    List<GateWay> groupList;
    List<List<SheBeiClass>> itemList ;
    private ArrayList<UserClass> user;
   // SwipeListView lt_permission;
    LinearLayout no_news;
    MyApplication m;
    private ArrayList<GateWay> Mygateway;
    TextView text_share;
    TextView text_Get;
    String UserName;
    public ExpandableListView el;
    IdeasExpandableListAdapter2 il;
    IdeasExpandableListAdapter2 i2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permissions);

       /* getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);*/
        info();
    }

    public void info() {
        SharedPreferences sp = getSharedPreferences("first", MODE_PRIVATE);
        UserName = sp.getString("username", null);
        no_news = (LinearLayout) findViewById(R.id.no_news);
        m = (MyApplication) getApplication();
        el = (ExpandableListView) findViewById(R.id.lt_expand);
        ImageView iv = (ImageView) findViewById(R.id.iv_my_baojing);
        text_share = (TextView) findViewById(R.id.text_share);
        text_Get = (TextView) findViewById(R.id.text_Get);
        SharePremission();
        text_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharePremission();
            }
        });


        text_Get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                GetPremission();

            }
        });


        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    public void SharePremission() {
        UserClass uc = new UserClass();
        uc.userId = m.userId;
        String js = JSON.toJSONString(uc);
        js = AESUtil.encrypt(js);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setContentType("application/json");
        try {
            params.setBodyEntity(new StringEntity(js, "utf-8"));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.deviceAuthorizedList, params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        HttpConnection.toastLog("网络错误", PermissionsActivity.this);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            Log.d("接受",arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String status = jb.getString("status");
                            if ("0".equals(status)) {
                                JSONArray ja = jb.getJSONArray("deviceList");
                                Mygateway = (ArrayList<GateWay>) JSON.parseArray(ja.toString(), GateWay.class);
                                initData();
                                el.setGroupIndicator(null);
                                el.setDivider(null);
                                if(i2!=null){
                                    i2.flag="0";
                                    i2.notifyDataSetChanged();
                                }else{
                                    i2 = new IdeasExpandableListAdapter2(PermissionsActivity.this,groupList,itemList,"0");
                                    el.setAdapter(i2);
                                }

                                if (Mygateway.size() != 0) {
                                    no_news.setVisibility(View.GONE);
                                    el.setVisibility(View.VISIBLE);
                                } else {
                                    no_news.setVisibility(View.VISIBLE);
                                    el.setVisibility(View.GONE);
                                }

                                if (Mygateway.size() != 0) {
                                    no_news.setVisibility(View.GONE);
                                    el.setVisibility(View.VISIBLE);
                                } else {
                                    no_news.setVisibility(View.VISIBLE);
                                    el.setVisibility(View.GONE);
                                }
                                text_share.setTextColor(Color.parseColor("#616161"));
                                text_Get.setTextColor(Color.parseColor("#c0c0c0"));
                            }
                        }else{
                            HttpConnection.showToast(PermissionsActivity.this,"服务器错误");
                        }

                    }
                });
    }


    public void GetPremission() {
        GateWay Gw = new GateWay();
        Gw.userId = m.userId;
        Gw.status = 2;
        String j = JSON.toJSONString(Gw);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.authorizeAcceptMacList, params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        HttpConnection.toastLog("网络异常", PermissionsActivity.this);
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            Log.d("共享",arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String status=jb.getString("status");
                            if("0".equals(status)){
                            JSONArray ja = jb.getJSONArray("deviceList");
                            Mygateway = (ArrayList<GateWay>) JSON.parseArray(ja.toString(), GateWay.class);
                               initData();
                               el.setGroupIndicator(null);
                               el.setDivider(null);
                                if(i2!=null){
                                    i2.flag="1";
                                    i2.notifyDataSetChanged();
                                }
                            if (Mygateway.size() != 0) {
                                no_news.setVisibility(View.GONE);
                                el.setVisibility(View.VISIBLE);
                            } else {
                                no_news.setVisibility(View.VISIBLE);
                                el.setVisibility(View.GONE);
                            }

                             text_share.setTextColor(Color.parseColor("#c0c0c0"));
                             text_Get.setTextColor(Color.parseColor("#616161"));}else{
                                HttpConnection.toastLog("服务器错误", PermissionsActivity.this);
                            }
                        } else {

                            HttpConnection.toastLog("服务器错误", PermissionsActivity.this);

                        }
                    }

                });


    }

    private void initData() {

        if(groupList==null){
            groupList=new ArrayList<GateWay>();
            itemList=new ArrayList<List<SheBeiClass>>();
        }else{
            groupList.clear();
            itemList.clear();
        }


        for(int i=0;i<Mygateway.size();i++){

            if(Mygateway.get(i).mac!=null){
                GateWay gt=new GateWay();
                gt.id=Mygateway.get(i).id;
                gt.status=Mygateway.get(i).status;
                gt.devices=Mygateway.get(i).devices;
                gt.mac=Mygateway.get(i).mac;
                gt.mobileFrom=Mygateway.get(i).mobileFrom;
                gt.mobileTo=Mygateway.get(i).mobileTo;
                groupList.add(gt);
            }else{
                GateWay gt=new GateWay();
                gt.status=Mygateway.get(i).status;
                gt.id=Mygateway.get(i).id;
                gt.devices=Mygateway.get(i).devices;
                gt.mac="设备集合";
                gt.mobileFrom=Mygateway.get(i).mobileFrom;
                gt.mobileTo=Mygateway.get(i).mobileTo;
                groupList.add(gt);
            }
        }

        for(int j=0;j<groupList.size();j++){
            itemList.add(groupList.get(j).devices);
        }
        Log.d("SIZE1",groupList.size()+"");
        Log.d("SIZE2",itemList.size()+"");

    }

}
